<?php
/**
 * Gd.php
 * 
 * @category   Naneau
 * @package    Naneau_Image
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/** base class */
require_once 'Naneau/Image/Adapter/Abstract.php';

/**
 * Naneau_Image_Adapter_Gd
 * 
 * image adapter for Gd
 *
 * @category   Naneau
 * @package    Naneau_Image
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Image_Adapter_Gd extends Naneau_Image_Adapter_Abstract {

    /**
     * process the file
     * @return void
     */
    protected function processFile() {

        if ($this->_mimeType == 'image/png') {
            $this->_handle = imagecreatefrompng($this->_fileName);
        } elseif ($this->_mimeType == 'image/gif') {
            $this->_handle = imagecreatefromgif($this->_fileName);
        } else {
            $this->_handle = imagecreatefromjpeg($this->_fileName);
        }
    }

    /**
     * get the width of the image
     * @return int
     */
    public function getWidth() {
        return imagesx($this->_handle);
    }

    /**
     * get the height of the image
     * @return int
     */
    public function getHeight() {
        return imagesy($this->_handle);
    }

    /**
     * resize the image
     *
     * @param int $x
     * @param int $y
     * @return void
     */
    public function resize($x, $y) {
        $newResource = imagecreatetruecolor($x, $y);
        //temp
        imagecopyresampled($newResource, $this->_handle, 0, 0, 0, 0, $x, $y, $this->getWidth(), $this->getHeight());
        //actual magic
        imagedestroy($this->_handle);
        //destroy original handle
        $this->_handle = $newResource;
        //replace handle
    }
    
    /**
     * crop the image
     *
     * @param int $x
     * @param int $y
     * @param int $width
     * @param int $height
     */
    public function crop($x, $y, $width, $height) {
        $newResource = imagecreatetruecolor($width, $height);
        //create a resource for the cropped result
        imagecopyresampled($newResource, $this->_handle, 0, 0, $x, $y, $width, $height, $width, $height);
        //actual magic
        imagedestroy($this->_handle);
        //destroy original handle
        $this->_handle = $newResource;
        //replace handle         
    }

    /**
     * watermark the image, using a watermark file
     *
     * @param string $watermarFile
     * @return void
     */
    public function waterMark($watermarkFile) {}

    /**
     * actual saving
     *
     * @param string $fileName
     * @param int $type
     * @return bool
     */
    public function _save($fileName, $type) {
        $fileHandle = fopen($fileName, 'w');
        if (!$fileHandle) {
            throw new Exception('Could not open file for writing: ' . $fileName);
        }
        //open file handle

        return fwrite($fileHandle, $this->getString($type));
        //write the file
    }

    /**
     * get the image as a string
     * @param int $type
     * @return string
     */
    public function getString($type) {
        ob_start();
        switch ($type) {
            case Naneau_Image::TYPE_JPEG :
                imagejpeg($this->_handle, null, 95);
                break;
            case Naneau_Image::TYPE_GIF :
                imagegif($this->_handle);
                break;
            case Naneau_Image::TYPE_PNG :
                imagepng($this->_handle);
                break;
        }
        $image = ob_get_contents();
        ob_end_clean();
        return $image;
    }
}